package com.cms.biz.category;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import com.cms.bean.CategoryBean;
import com.cms.util.ApplicationHelper;

public class CategoryHelper {
	
	/**
	 * 根据类型ID得到类别
	 * @param conn
	 * @param categoryTypeId
	 * @return
	 */
	@SuppressWarnings("finally")
	public static List<CategoryBean> getCategoryListByTypeId(Connection conn,
			int categoryTypeId) {
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<CategoryBean> list = new ArrayList<CategoryBean>();
		try {
			pst = conn.prepareStatement(SQLString.GET_CATEGORY_BY_TYPE_ID);
			pst.setInt(1, categoryTypeId);
			rs = pst.executeQuery();
			while (rs.next()) {
				CategoryBean cBean = new CategoryBean();
				cBean.setId(rs.getInt("id"));
				cBean.setName(rs.getString("category_name"));
				cBean.setParentId(rs.getInt("parent_id"));
				cBean.setTypeId(rs.getInt("type_id"));
				cBean.setChildCategoryBeans(getCategoryListByParentId(conn,cBean.getId()));
				list.add(cBean);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
					rs = null;
				} catch (final SQLException e) {
				}
			}
			if (pst != null) {
				try {
					pst.close();
					pst = null;
				} catch (final SQLException e) {
				}
			}
			return list;
		}
	}
	/**
	 * 根据父类ID取得子类
	 * @param conn
	 * @param parentId
	 * @return
	 */
	@SuppressWarnings("finally")
	public static List<CategoryBean> getCategoryListByParentId(Connection conn,
			int parentId) {
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<CategoryBean> list = new ArrayList<CategoryBean>();
		try {
			pst = conn.prepareStatement(SQLString.GET_CATEGORY_BY_PARENT_ID);
			pst.setInt(1, parentId);
			rs = pst.executeQuery();
			while (rs.next()) {
				CategoryBean cBean = new CategoryBean();
				cBean.setId(rs.getInt("id"));
				cBean.setName(rs.getString("category_name"));
				cBean.setParentId(rs.getInt("parent_id"));
				cBean.setTypeId(rs.getInt("type_id"));
				list.add(cBean);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
					rs = null;
				} catch (final SQLException e) {
				}
			}
			if (pst != null) {
				try {
					pst.close();
					pst = null;
				} catch (final SQLException e) {
				}
			}
			return list;
		}
	}
	
	/**
	 * 根据类型ID和类别ID得到类别BEAN
	 * @param session
	 * @param categoryTypeId
	 * @param id
	 * @return
	 */
	public static CategoryBean getCategoryBeanById(HttpSession session,
			int categoryTypeId, int id) {
		List<CategoryBean> list = ApplicationHelper
				.getApplicationSubCategoryListById(session, categoryTypeId);
		if (list != null && list.size() > 0) {
			for (CategoryBean cb : list) {
				CategoryBean node = cb.getCategoryBeanById(id);
				if (node != null) {
					return node;
				}
			}
		}
		return null;
	}

	/**
	 * 根据类别ID得到类别BEAN
	 * @param list
	 * @param id
	 * @return
	 */
	public static CategoryBean getCategoryBeanById(List<CategoryBean> list, int id) {
		if (list != null && list.size() > 0) {
			for (CategoryBean cb : list) {
				CategoryBean node = cb.getCategoryBeanById(id);
				if (node != null) {
					return node;
				}
			}
		}
		return null;
	}
}
